﻿/*
 * Author：步尘
 * CreatDate ：2018-10-01 09:23:37
 * CLR Version ：4.0.30319.42000
 */
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using WimMain.Common;
using WimMain.Models;

namespace WimMain.Fun
{
    /// <summary>
    /// 所有可执行方法的父类
    /// </summary>
    public abstract class BaseFun
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contentStr">连接字符串</param>
        public BaseFun(string contentStr) => DbContent = DBHelper.GetDbContent(contentStr);

        #region property

        /// <summary>
        /// 数据库连接对象
        /// </summary>
        public DBHelper DbContent { get; set; }

        /// <summary>
        /// 链接字符串列表
        /// </summary>
        public List<KeyVal> ContentStringList { get; set; }

        /// <summary>
        /// 返回结果
        /// </summary>
        public Result Res { get; set; }

        /// <summary>
        /// 日志初始路径
        /// </summary>
        private static string LogStarPath => ToolFile.GetAbsolutelyPath(ToolConfig.GetAppSetting("LogStarPath"));

        /// <summary>
        /// 日志路径
        /// </summary>
        public static string LogPath { get; set; }

        /// <summary>
        /// 数据文件初始路径
        /// </summary>
        private static string DataStarPath => ToolFile.GetAbsolutelyPath(ToolConfig.GetAppSetting("DataStarPath"));

        /// <summary>
        /// 数据路径
        /// </summary>
        public static string DataPath { get; set; }


        /// <summary>
        /// 分割线
        /// </summary>
        protected string SegmentingLine => "=========={0}==========";

        #endregion

        /// <summary>
        /// 运行
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public Result RunFun(Func<Result> func)
        {
            Res = new Result();
            LogPath = LogStarPath + GetMethodName(2) + ".log";
            DataPath = DataStarPath + GetMethodName(2) + "\\";

            WriteLog(LogPath, string.Format(SegmentingLine, "Star"));

            // 执行方法
            Res.RunTime = this.Watch(() => func.Invoke());

            WriteLog(LogPath, string.Format(SegmentingLine, "End ") + "\t" + Res.RunTime + " ms\n");
            return Res;
        }

        /// <summary>
        /// 获得当前方法名称
        /// </summary>
        /// <param name="index">堆栈帧</param>
        /// <returns></returns>
        protected string GetMethodName(int index = 3)
        {
            StackTrace ss = new StackTrace(true);
            MethodBase mb = ss.GetFrame(index).GetMethod();
            return mb.DeclaringType.Name + "\\" + mb.Name;
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="logPath">日志路径</param>
        /// <param name="msg">日志消息</param>
        /// <param name="append">是否追加</param>
        /// <param name="hasDate">是否包含时间</param>
        protected void WriteLog(string logPath, string msg, bool append = true, bool hasDate = true)
        {
            logPath = ToolFile.GetAbsolutelyPath(logPath);
            if (File.Exists(logPath) && !append)
                File.Delete(logPath);

            ToolFile.CreatFile(logPath, (hasDate ? (DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")) + "\t\t" : "") + msg, true);
        }

        /// <summary>
        /// 计算运算时间
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        protected long Watch(Action action)
        {
            // 定时器
            Stopwatch watch = new Stopwatch();
            // 开始计时
            watch.Start();
            action.Invoke();
            // 停止计时
            watch.Stop();
            // 返回运行时间
            return watch.ElapsedMilliseconds;
        }

    }
}